package com.electromagnetic.industry.software.manage.controller;

import com.electromagnetic.industry.software.common.annotations.UserOperation;
import com.electromagnetic.industry.software.common.enums.UserOperationModuleEnum;
import com.electromagnetic.industry.software.common.resp.ElectromagneticResult;
import com.electromagnetic.industry.software.common.util.ElectromagneticResultUtil;
import com.electromagnetic.industry.software.manage.service.ImportPrjService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Optional;

@RestController
@RequestMapping("/data/ed/local/prj")
public class ImportPrjController {

    @Resource
    private ImportPrjService importPrjService;

    @RequestMapping("/importPrj")
    @UserOperation(value = "导入本地工程", modelName = UserOperationModuleEnum.IMPORT_PRJ)
    public ElectromagneticResult<?> importPrj(@RequestParam("file") MultipartFile file) {
        return ElectromagneticResultUtil.success(importPrjService.importPrj(file));
    }

    @RequestMapping("/queryPrjInfo")
    @UserOperation(value = "查询导入工程列表", modelName = UserOperationModuleEnum.IMPORT_PRJ)
    public ElectromagneticResult<?> queryPrjInfo(@RequestParam(value = "pageNum", required = false) Integer pageNum,
                                                 @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        pageNum = Optional.ofNullable(pageNum).orElse(1);
        pageSize = Optional.ofNullable(pageSize).orElse(Integer.MAX_VALUE);
        return ElectromagneticResultUtil.success(importPrjService.queryPrjInfo(pageNum, pageSize));
    }

    @RequestMapping("/queryImportFileInfo")
    @UserOperation(value = "查询导入工程文件和文件夹信息", modelName = UserOperationModuleEnum.IMPORT_PRJ)
    public ElectromagneticResult<?> queryImportFileInfo(@RequestParam(value = "pageNum") Integer pageNum,
                                                        @RequestParam(value = "pageSize") Integer pageSize,
                                                        @RequestParam("id") String id) {
        return ElectromagneticResultUtil.success(importPrjService.queryImportFileInfo(id, pageNum, pageSize));
    }

    @RequestMapping("/download")
    @UserOperation(value = "下载工程文件", modelName = UserOperationModuleEnum.IMPORT_PRJ)
    public ResponseEntity<InputStreamResource> download(@RequestParam("id") String id,
                                                        HttpServletResponse response) {
        return importPrjService.download(id, response);
    }

    @RequestMapping("/removePrj")
    @UserOperation(value = "删除工程", modelName = UserOperationModuleEnum.IMPORT_PRJ)
    public ElectromagneticResult<?> removePrj(@RequestParam("prjId") String prjId) {
        return ElectromagneticResultUtil.success(importPrjService.removePrj(prjId));
    }


    @RequestMapping("/tree")
    @UserOperation(value = "查询工程树", modelName = UserOperationModuleEnum.IMPORT_PRJ)
    public ElectromagneticResult<?> tree(@RequestParam("prjId") String prjId) {
        return ElectromagneticResultUtil.success(importPrjService.tree(prjId));
    }

}
